package toanstt.gameplay;


import toanstt.com.T;
import toanstt.com.Vec2;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PathDashPathEffect.Style;
import android.widget.TextView.BufferType;

public class Cell 
{
	//Body body;
	int x;
	int y;
	int v_y;
	int r,g,b;
	int cell;
	int col;
	int step_temp;
	int lable = 0;
	int index;
	int bin;
	int prev_cell[];
	int n_pre;
	int int_temp;
	int lucky=1;
	public Cell(int bin2,int index2)
	{
		n_pre = 0;
		prev_cell = new int[20];
		bin = bin2;
		//r = T.random.nextInt(255);
		//g = T.random.nextInt(255);
		//b = T.random.nextInt(255);
		//body = PhysicsWorld.New_Dynamic_Body_Rect(T.GAMESCREEN_MIN_X+ bin*T.CELL_WIDTH+T.CELL_RADIUS, -T.CELL_RADIUS, T.CELL_RADIUS-1, 0, T.random.nextInt(100));
		x = T.GAMESCREEN_MIN_X+ bin*T.CELL_WIDTH+T.CELL_RADIUS;
		y = -T.CELL_RADIUS-1;
		v_y = 2*T.V_Y + T.random.nextInt(T.V_Y);
		index = index2;
		
		int_temp=T.GetProbality(T.PLAY_UNLUCKY); 
		if(int_temp>0)
		{
			if(int_temp ==1)
				lable = 11;
			else
				lable =12;
		}
		else
		{
			lable = T.random.nextInt(T.GAME_NUM_DIAMOND);
			int_temp=T.GetProbality(T.PLAY_LUCKY); 
			if(int_temp>0)
			{
				if(int_temp ==1)
					lucky =2;
				else
					if(int_temp ==2)
						lucky =3;
				else
					lucky =3;
			}
		}
	}
	public void Update2()
	{
		int indextemp = -1;
		int length_temp=100000;
		int d =0;
		int temp_int =CellTable.GetINdexcellwithcoor(x,y+v_y+T.CELL_RADIUS-1);
		int y_temp  = T.GAMESCREEN_MIN_Y + CellTable.GetROWwithcoor(y+v_y+T.CELL_RADIUS-1)*T.CELL_HEIGHT + T.CELL_RADIUS;
		n_pre = 0;
		for(int i=0;i<121;i++)
		{
			
			if(CellTable.cell[i] == null || i == index || CellTable.cell[i].bin != bin)
				continue;
			
			if(CellTable.cell[i].bin == bin)
			{
				d = CellTable.cell[i].y -y;
				if(d<0)
				{
					if(CellTable.cell[i].y < T.GAMESCREEN_MIN_Y + T.CELL_RADIUS)
						continue;
					if(CellTable.cell[i]==null)
						continue;
					
					if(d < -T.CELL_RADIUS)
					{
						prev_cell[n_pre] = i;
						n_pre++;
					}
					continue;
					
				}
				 
				
				if(d>=0)
					if(d<length_temp)
					{
						length_temp = d;
						indextemp = i;
					}
			}
		}
		if(indextemp==-1)
		{
			if(temp_int == -2)
			{
				d = y_temp - T.CELL_HEIGHT -y;
				y +=d ;
				//T.sound.Play(T.sound.HIT);
				for(int j=0;j<n_pre;j++)
					CellTable.cell[prev_cell[j]].y+= d;
				return;
				
			}
			if(y+v_y>T.GAMESCREEN_MAX_Y-T.CELL_RADIUS)
			{
				d = T.GAMESCREEN_MAX_Y-T.CELL_RADIUS -y;
				y += d;
				for(int j=0;j<n_pre;j++)
					CellTable.cell[prev_cell[j]].y+= d;
				v_y =0;
				//T.sound.Play(T.sound.HIT);
				return;
			}
			else
			{
				y+=v_y;
				d = v_y;
				for(int j=0;j<n_pre;j++)
					CellTable.cell[prev_cell[j]].y+= d;
				v_y +=  T.V_Y;
				if(v_y>=T.CELL_WIDTH)
					v_y = T.CELL_WIDTH-1;
				return;
			}
			
			
		}
		else if(length_temp>T.CELL_HEIGHT)
		{
			d = length_temp - T.CELL_HEIGHT;
			if(d > v_y)
			{
				y+=v_y;	
				for(int j=0;j<n_pre;j++)
					CellTable.cell[prev_cell[j]].y+= v_y;
				
				v_y +=  T.V_Y;
				if(v_y>=T.CELL_WIDTH)
					v_y = T.CELL_WIDTH-1;
				return;
			}
			else if(d <= v_y)
			{
				y+=d;
				
				for(int j=0;j<n_pre;j++)
					CellTable.cell[prev_cell[j]].y+= d;
				//v_y =0;
				T.sound.Play(T.sound.HIT);
				return;
			}
		}
		
	
	}
	public void Update()
	{
		//if(v_y==0)
			//return;
		n_pre =0;
		for(int i =0;i<121;i++)
		{
			
			if(CellTable.cell[i] !=null && y-CellTable.cell[i].y > T.CELL_HEIGHT &&CellTable.cell[i].y>T.CELL_RADIUS )
			{
				prev_cell[n_pre] = i;
				n_pre++;
			}
		}
		
		int temp_int =CellTable.GetINdexcellwithcoor(x,y+v_y+T.CELL_RADIUS-1);
		
		if(temp_int!=-1)
		{
			if(temp_int == index)
			{
				y+=v_y;	
				//a
				for(int j=0;j<n_pre;j++)
					CellTable.cell[prev_cell[j]].y+= v_y;
				//end a
				v_y +=  T.V_Y;
				if(v_y>=T.CELL_WIDTH)
					v_y = T.CELL_WIDTH-1;
				return;
			}
			else
			{
				//return;
				
				Vec2 temp =CellTable.Getcellwithcoor(x,y+v_y+T.CELL_RADIUS-1);
				y = (temp.y-1)*T.CELL_HEIGHT + T.CELL_RADIUS ;
				v_y =0;
				////T.sound.Play(T.sound.HIT);
				//return;
			}
		}
		else if(y+v_y>T.GAMESCREEN_MAX_Y-T.CELL_RADIUS)
			{
				y = T.GAMESCREEN_MAX_Y-T.CELL_RADIUS;
				v_y =0;
				////T.sound.Play(T.sound.HIT);
				//return;
			}
		else
		{
			y+=v_y;	
			//a
			for(int j=0;j<n_pre;j++)
				CellTable.cell[prev_cell[j]].y+= v_y;
			//end a
			v_y +=  T.V_Y;
			if(v_y>=T.CELL_WIDTH)
				v_y = T.CELL_WIDTH-1;
			//return;
			////T.sound.Play(T.sound.HIT);
		}
	}
	
	public void Draw(Canvas canvas)
	{
		T.mat.reset();
		//T.mat = new Matrix();
		T.mat.postScale(T.CELL_WIDTH*1.0f/60, T.CELL_WIDTH*1.0f/60);
		T.mat.postTranslate(x-T.CELL_RADIUS,  y-T.CELL_RADIUS);
		//T.paint.setColor(Color.rgb(r, g, b));
		//canvas.drawRect(x-T.CELL_RADIUS, y-T.CELL_RADIUS,x+T.CELL_RADIUS, y+T.CELL_RADIUS, T.paint);
		
		canvas.drawBitmap(T.bitmap_diamond[lable], T.mat, null);
		
		//T.paint.setColor(Color.GREEN);
		
		//canvas.drawText("x"+lucky, x+T.CELL_QUAD, y + T.CELL_QUAD, T.paint);
		//T.mat.reset();
		if(lucky > 1)
		{
			T.mat.postTranslate(T.CELL_RADIUS,T.CELL_RADIUS);
			canvas.drawBitmap(T.bitmap_diamond_x[lucky-2], T.mat, null);
		}
	}
}
